//
//  BookingViewController.m
//  OfficeBook
//
//  Created by 孙晓奇 on 15/9/22.
//  Copyright © 2015年 sunny. All rights reserved.
//

#import "BookingViewController.h"
#import "FloorView.h"
#import "CalendarView.h"
#import "BookingConfirmViewController.h"
#import "BookingEditViewController.h"
#import "BookingEditTransitionAnimator.h"
#import "BookingConfirmTransitionAnimator.h"
#import "UserInfoViewController.h"
#import "LoginViewController.h"
#import "FilterViewController.h"
#import "FilterTransitionAnimator.h"
#import "BookingInfoModel.h"
#import "CalendarEvent.h"
#import "PresentModalHelper.h"

@interface BookingViewController()
<UIViewControllerTransitioningDelegate, FloorViewDelegate, CalendarViewDelegate,
 BookingConfirmControllerDelegate, BookingEditControllerDelegate, FilterControllerDelegate>

@property (nonatomic, strong) FloorView    *floorView;
@property (nonatomic, strong) CalendarView *calendarView;

@property (nonatomic, strong) BookingInfoModel *bookingInfo;
@property (nonatomic, strong) CalendarEvent    *calEvent;

//layout
@property (nonatomic, assign) CGFloat  calendarViewHeight;
@end

@implementation BookingViewController


-(instancetype)init {
    self = [super init];
    if (self) {
        self.calendarViewHeight = 120;
        self.bookingInfo = [[BookingInfoModel alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"Conference Rooms";
    
    [self initNavBarBtnItem];
    
    // add FloorView
    CGRect frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - self.calendarViewHeight - 64.f);
    self.floorView = [[FloorView alloc] initWithFrame:frame];
    self.floorView.delegate = self;
    [self.view addSubview:self.floorView];
    
    // add CalendarView
    frame = CGRectMake(0, CGRectGetMaxY(self.floorView.frame), CGRectGetWidth(self.view.frame), self.calendarViewHeight);
    self.calendarView = [[CalendarView alloc] initWithFrame:frame];
    self.calendarView.selectedEventInfo = self.bookingInfo;
    self.calendarView.delegate = self;
    [self.view addSubview:self.calendarView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - FloorView Delegate
-(void)floorView:(FloorView *)floorView didSelectedMeetingRoom:(RoomModel *)roomInfo {
    self.bookingInfo.roomInfo = roomInfo;
    
    BookingConfirmViewController *vc = [[BookingConfirmViewController alloc] init];
    UINavigationController *toNav = [NavigationControllerHelper initNavWithRootViewController:vc];
    
    vc.delegate = self;
    vc.bookingInfo = self.bookingInfo;
    
    toNav.modalPresentationStyle = UIModalPresentationCustom;
    toNav.transitioningDelegate = self;
    [self presentViewController:toNav animated:YES completion:nil];
}

#pragma mark - CalendarView Delegate
-(void)view:(CalendarView *)calendarView didSelectedCalendarEvent:(CalendarEvent *)event withBookingInfo:(BookingInfoModel *)bookingInfo {
    self.calEvent = event;
    //modal
    BookingEditViewController *vc = [[BookingEditViewController alloc] init];
    UINavigationController *toNav = [NavigationControllerHelper initNavWithRootViewController:vc];
    
    vc.delegate = self;
    vc.bookingInfo = bookingInfo;
    
    toNav.modalPresentationStyle = UIModalPresentationCustom;
    toNav.transitioningDelegate = self;
    [self presentViewController:toNav animated:YES completion:nil];
}

#pragma mark - BookingConfirmControllerDelegate
-(void)viewController:(BookingConfirmViewController *)controller didFinishBooking:(BookingInfoModel *)bookingInfo {
    //add code here
}

#pragma mark - BookingEditControllerDelegate
-(void)viewController:(BookingEditViewController *)vc didFinishEditing:(BookingInfoModel *)editedInfo{
    self.bookingInfo = editedInfo;
    [self.calendarView calendarEvent:self.calEvent updatedBookingInfo:editedInfo];
}

-(void)viewController:(BookingEditViewController *)vc deleteBooking:(BOOL)del {
    if (del == YES) {
        [self.calendarView deleteCalendarEvent:self.calEvent];
    }
}

#pragma mark - layout
-(void)initNavBarBtnItem {
    UIButton *userBtn = [[UIButton alloc] initWithFrame:CGRectMake(11, 11, 22, 22)];
    [userBtn setImage:[UIImage imageNamed:@"navigationUserBtn"] forState:UIControlStateNormal];
    [userBtn addTarget:self action:@selector(toUser) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:userBtn];
    
    UIButton *filterBtn = [[UIButton alloc] initWithFrame:CGRectMake(11, 11, 22, 22)];
    [filterBtn setImage:[UIImage imageNamed:@"navigationFilterBtn"] forState:UIControlStateNormal];
    [filterBtn addTarget:self action:@selector(toFilter) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:filterBtn];
}

-(void)toUser{
    UIViewController *vc;
    if ([UserDefaultDataHelper getDataWithKey:UserDefaultKeyUserEmail] == nil) {
        vc = [[LoginViewController alloc] init];
    } else {
        vc = [[UserInfoViewController alloc] init];
    }
    UINavigationController *nav = [NavigationControllerHelper initNavWithRootViewController:vc];
    
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}

-(void)toFilter{
    FilterViewController *vc = [[FilterViewController alloc] init];
    vc.delegate = self;
    
    UINavigationController *toNav = [NavigationControllerHelper initNavWithRootViewController:vc];
    toNav.modalPresentationStyle = UIModalPresentationCustom;
    toNav.transitioningDelegate = self;
    [self presentViewController:toNav animated:YES completion:nil];
}

#pragma mark - FilterControllerDelegate 
-(void)viewController:(FilterViewController *)controller didFinishFiltration:(FilterModel *)filter {
    [self.floorView refreshRoomList:filter];
}

#pragma mark - UIViewControllerTransitioningDelegate
/*
 Called when presenting a view controller that has a transitioningDelegate
 */
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                      sourceController:(UIViewController *)source
{
    id<UIViewControllerAnimatedTransitioning> animationController;
    
    //BookingConfirm
    if ([presented isKindOfClass:[UINavigationController class]] &&
        [((UINavigationController *)presented).topViewController isKindOfClass:[BookingConfirmViewController class]]) {
        BookingConfirmTransitionAnimator *animator = [[BookingConfirmTransitionAnimator alloc] init];
        animator.appearing = YES;
        animator.duration = 0.15;
        animationController = animator;
    }
    //BookingEdit
    else if ([presented isKindOfClass:[UINavigationController class]] &&
        [((UINavigationController *)presented).topViewController isKindOfClass:[BookingEditViewController class]]) {
        BookingEditTransitionAnimator *animator = [[BookingEditTransitionAnimator alloc] init];
        animator.appearing = YES;
        animator.duration = 0.25;
        animator.vcHeight = self.calendarViewHeight;
        animationController = animator;
    }
    //Filter
    else if ([presented isKindOfClass:[UINavigationController class]] &&
             [((UINavigationController *)presented).topViewController isKindOfClass:[FilterViewController class]]) {
        FilterTransitionAnimator *animator = [[FilterTransitionAnimator alloc] init];
        animator.appearing = YES;
        animator.duration = 0.3f;
        animationController = animator;
    }
    return animationController;
}

/*
 Called when dismissing a view controller that has a transitioningDelegate
 */
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    id<UIViewControllerAnimatedTransitioning> animationController;

    // BookingConfirm
    if ([dismissed isKindOfClass:[UINavigationController class]] &&
        [((UINavigationController *)dismissed).topViewController isKindOfClass:[BookingConfirmViewController class]]) {
        BookingConfirmTransitionAnimator *animator = [[BookingConfirmTransitionAnimator alloc] init];
        animator.appearing = NO;
        animator.duration = 0.10;
        animationController = animator;
    }
    //BookingEdit
    else if ([dismissed isKindOfClass:[UINavigationController class]] &&
             [((UINavigationController *)dismissed).topViewController isKindOfClass:[BookingEditViewController class]]) {
        BookingEditTransitionAnimator *animator = [[BookingEditTransitionAnimator alloc] init];
        animator.appearing = NO;
        animator.duration = 0.25;
        animationController = animator;
    }
    //Filter
    else if ([dismissed isKindOfClass:[UINavigationController class]] &&
             [((UINavigationController *)dismissed).topViewController isKindOfClass:[FilterViewController class]]) {
        FilterTransitionAnimator *animator = [[FilterTransitionAnimator alloc] init];
        animator.appearing = NO;
        animator.duration = 0.30;
        animationController = animator;
    }
    return animationController;
}
@end
